package com.onesports.intelligent.k12.polarlight.service.coach;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.*;
import com.onesports.intelligent.k12.polarlight.domain.vo.organization.CoachServeVO;
import com.onesports.intelligent.k12.polarlight.mapper.organization.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Wsy
 **/
@Service
public class CoachManager extends ServiceImpl<CoachMapper, Coach> {

    @Resource
    private CoachMapper coachMapper;

    @Resource
    private CoachCampusRelService coachCampusRelService;


    /**
     * 获取教练服务信息
     *
     * @param courseId            课程id
     * @param campusId            校区id
     * @param scheduleTemplateIds 排班表id
     * @return List<CoachServeVO>
     */
    public List<CoachServeVO> serveInfo(String courseId, String campusId, List<String> scheduleTemplateIds, List<String> statusList) {
        return this.coachMapper.serveInfo(courseId, campusId, scheduleTemplateIds, statusList);
    }

    public void syncCampus(String staffId,List<String> campusIds,String organizationId) {
        if(StringUtils.isBlank(staffId)){
            return;
        }
        List<Coach> coaches = coachMapper.selectList(new LambdaQueryWrapper<Coach>().eq(Coach::getOrganizationStaffId,staffId));
        if(CollectionUtils.isNotEmpty(coaches)){
            coaches.forEach(coach -> {
                List<CoachCampusRel> coachCampusRels = coachCampusRelService.findByCoachId(coach.getId());
                if(CollectionUtils.isNotEmpty(coachCampusRels)){
                    //要删除的关联
                    List<CoachCampusRel> removeList = coachCampusRels.stream().filter(item -> !campusIds.contains(item.getCampusId())).collect(Collectors.toList());
                    if(CollectionUtils.isNotEmpty(removeList)){
                        coachCampusRelService.removeByIds(removeList.stream().map(CoachCampusRel::getId).collect(Collectors.toList()));
                    }
                    //已关联的校区ID
                    List<String> useCampusIds = coachCampusRels.stream().map(CoachCampusRel::getCampusId).collect(Collectors.toList());
                    //要新增的校区关联
                    List<String> addCampusIds = campusIds.stream().filter(item -> !useCampusIds.contains(item)).collect(Collectors.toList());

                    if(CollectionUtils.isNotEmpty(addCampusIds)){
                        List<CoachCampusRel> addCoachCampusRel = Lists.newArrayList();
                        addCampusIds.forEach(item -> {
                            CoachCampusRel rel = new CoachCampusRel();
                            rel.setCoachId(coach.getId());
                            rel.setOrganizationId(organizationId);
                            rel.setCampusId(item);
                            rel.setQueueNumber(coachCampusRelService.selectCoachNumber(item).intValue()+1);
                            addCoachCampusRel.add(rel);
                        });

                        if(CollectionUtils.isNotEmpty(addCoachCampusRel)){
                            coachCampusRelService.saveBatch(addCoachCampusRel);
                        }
                    }
                }
            });
        }
    }

}
